<?php

/**
 * @property string $area_id
 * @property string $pid
 * @property string $area_name
 * @property string $area_code
 * @property integer $area_type
 * @property string $letter
 * @property string $pinyin
 * @property string $create_date
 * @property string $update_date
 * @author lovebing <lovebing365@126.com> Created on 2015-2-9
 */
class AreaModel extends MainActiveRecord {

    const AREA_TYPE_PROVINCE = 1;
    const AREA_TYPE_CITY = 2;

    private static $names = array();

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'ai9me_area';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('area_type', 'numerical', 'integerOnly' => true),
            array('pid', 'length', 'max' => 11),
            array('area_name', 'length', 'max' => 200),
            array('area_code', 'length', 'max' => 8),
            array('letter', 'length', 'max' => 1),
            array('pinyin', 'length', 'max' => 100),
            array('create_date, update_date', 'safe'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('area_id, pid, area_name, area_code, area_type, letter, pinyin, create_date, update_date', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'area_id' => 'Area',
            'pid' => 'Pid',
            'area_name' => 'Area Name',
            'area_code' => 'Area Code',
            'area_type' => 'Area Type',
            'letter' => 'Letter',
            'pinyin' => 'Pinyin',
            'create_date' => 'Create Date',
            'update_date' => 'Update Date',
        );
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return AreaModel the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public static function getList($pid, $areaType) {
        $model = self::model();
        $table = $model->tableName();
        $command = $model->getDbConnection()->createCommand();
        $field = 'area_id, area_name';
        return $command->select($field)
                        ->from($table)
                        ->where("pid = {$pid} and area_type = {$areaType}")
                        ->queryAll();
    }

    public static function getProvinceList() {
        return self::getList(1, 1);
    }

    public static function getCityList($provinceId) {
        return self::getList($provinceId, 2);
    }

    public static function getDistrictList($cityId) {
        return self::getList($cityId, 3);
    }

    public static function getListHainan($pid, $areaType) {
        $model = self::model();
        $table = $model->tableName();
        $command = $model->getDbConnection()->createCommand();
        $field = 'area_id, area_name';
        return $command->select($field)
                        ->from($table)
                        ->where("pid = {$pid} and area_id = {$areaType}")
                        ->queryAll();
    }

    public static function getProvinceHaiNanList() {
        return self::getListHainan(1, 9);
    }

    public static function getCityHaiNanList() {
        return self::getList(9, 2);
    }

    /**
     * 
     * @param type $id
     * @return type
     */
    public static function getName($id) {
        if (!isset(self::$names[$id])) {
            $model = self::model()->findByPk($id);
            $name = '';
            if ($model) {
                $name = $model->area_name;
            }
            self::$names[$id] = $name;
        }
        return self::$names[$id];
    }

    /**
     * 
     * @param type $idArray
     * @return type
     */
    public static function getNames($idArray) {
        $ids = implode(',', $idArray);
        $models = array();
        if ($ids) {
            $models = self::model()->findAll("area_id in({$ids})");
        }
        foreach ($models as $model) {
            self::$names[$model->area_id] = $model->area_name;
        }
        return self::$names;
    }

    /**
     * 
     * @param type $city
     * @return self
     */
    public static function findCityByName($city) {
        $c = new CDbCriteria();
        $name = str_replace(['市', '自治州'], ['', ''], $city);
        $c->addCondition([
            'area_type' => self::AREA_TYPE_CITY
        ]);
        $c->addSearchCondition('area_name', $name);
        return self::model()->find($c);
    }

}
